﻿//==============================================================================
// File: WindowManagerExt.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Windows;
using Caliburn.PresentationFramework.ApplicationModel;
using WpfRcp.Core.Services;
using WpfRcp.Core.Views;

namespace WpfRcp.Services
{
    public class WindowManagerExt : IWindowManagerExt
    {
        private readonly IWindowManager m_WindowManager;
        private readonly IShell m_Shell;

        public WindowManagerExt(IWindowManager windowManager, IShell shell)
        {
            m_WindowManager = windowManager;
            m_Shell = shell;
        }

        public void Show(object rootModel, object context, Action<ISubordinate, Action> handleShutdownModel)
        {
            m_WindowManager.Show(rootModel, context, handleShutdownModel);
        }

        public bool? ShowDialog(object rootModel, object context, Action<ISubordinate, Action> handleShutdownModel)
        {
            return m_WindowManager.ShowDialog(rootModel, context, handleShutdownModel);
        }

        public object GetWindow(object view)
        {
            if (view == null)
                throw new ArgumentNullException("view");

            if (view is DependencyObject)
            {
                var window = Window.GetWindow((DependencyObject)view);
                if (window != null && window != view && window.IsLoaded) return window;

                return m_Shell;
            }

            return null;
        }

        public object GetOwnerWindow(object view)
        {
            var window = GetWindow(view) as Window;
            if (window != null && window.IsLoaded)
            {
                return window;
            }

            return m_Shell;
        }
    }
}